<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
  <title>Description of v_polygonxline</title>
  <meta name="keywords" content="v_polygonxline">
  <meta name="description" content="V_POLYGONXLINE Find where a line crosses a polygon [xc,ec,tc,xy0]=(p,l)">
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta name="generator" content="m2html &copy; 2003 Guillaume Flandin">
  <meta name="robots" content="index, follow">
  <link type="text/css" rel="stylesheet" href="../m2html.css">
</head>
<body>
<a name="_top"></a>
<div><a href="../index.html">Home</a> &gt;  <a href="index.html">v_mfiles</a> &gt; v_polygonxline.m</div>

<!--<table width="100%"><tr><td align="left"><a href="../index.html"><img alt="<" border="0" src="../left.png">&nbsp;Master index</a></td>
<td align="right"><a href="index.html">Index for v_mfiles&nbsp;<img alt=">" border="0" src="../right.png"></a></td></tr></table>-->

<h1>v_polygonxline
</h1>

<h2><a name="_name"></a>PURPOSE <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
<div class="box"><strong>V_POLYGONXLINE Find where a line crosses a polygon [xc,ec,tc,xy0]=(p,l)</strong></div>

<h2><a name="_synopsis"></a>SYNOPSIS <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
<div class="box"><strong>function [xc,ec,tc,xy0]=v_polygonxline(p,l) </strong></div>

<h2><a name="_description"></a>DESCRIPTION <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
<div class="fragment"><pre class="comment">V_POLYGONXLINE Find where a line crosses a polygon [xc,ec,tc,xy0]=(p,l)
 Inputs
 P(n,2) gives the polygon vertices
 L(1,3) gives the line in the form L*[X; Y; 1]=0

 Outputs
 xc(k,2) gives (x,y) coordinates of crossing points
 the crossings are between vertex pairs ec(k,1) and ec(k,1)+1
 tc(k,1) gives parametric position of crossing points (x,y)=(x0,y0)+tc(-l(2),l(1))
 xy0(1,2) gives the starting point, (x0,y0) of the parametric line</pre></div>

<!-- crossreference -->
<h2><a name="_cross"></a>CROSS-REFERENCE INFORMATION <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
This function calls:
<ul style="list-style-image:url(../matlabicon.gif)">
</ul>
This function is called by:
<ul style="list-style-image:url(../matlabicon.gif)">
</ul>
<!-- crossreference -->


<h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
<div class="fragment"><pre>0001 <a name="_sub0" href="#_subfunctions" class="code">function [xc,ec,tc,xy0]=v_polygonxline(p,l)</a>
0002 <span class="comment">%V_POLYGONXLINE Find where a line crosses a polygon [xc,ec,tc,xy0]=(p,l)</span>
0003 <span class="comment">% Inputs</span>
0004 <span class="comment">% P(n,2) gives the polygon vertices</span>
0005 <span class="comment">% L(1,3) gives the line in the form L*[X; Y; 1]=0</span>
0006 <span class="comment">%</span>
0007 <span class="comment">% Outputs</span>
0008 <span class="comment">% xc(k,2) gives (x,y) coordinates of crossing points</span>
0009 <span class="comment">% the crossings are between vertex pairs ec(k,1) and ec(k,1)+1</span>
0010 <span class="comment">% tc(k,1) gives parametric position of crossing points (x,y)=(x0,y0)+tc(-l(2),l(1))</span>
0011 <span class="comment">% xy0(1,2) gives the starting point, (x0,y0) of the parametric line</span>
0012 
0013 <span class="comment">%      Copyright (C) Mike Brookes 2009</span>
0014 <span class="comment">%      Version: $Id: v_polygonxline.m 10865 2018-09-21 17:22:45Z dmb $</span>
0015 <span class="comment">%</span>
0016 <span class="comment">%   VOICEBOX is a MATLAB toolbox for speech processing.</span>
0017 <span class="comment">%   Home page: http://www.ee.ic.ac.uk/hp/staff/dmb/voicebox/voicebox.html</span>
0018 <span class="comment">%</span>
0019 <span class="comment">%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</span>
0020 <span class="comment">%   This program is free software; you can redistribute it and/or modify</span>
0021 <span class="comment">%   it under the terms of the GNU General Public License as published by</span>
0022 <span class="comment">%   the Free Software Foundation; either version 2 of the License, or</span>
0023 <span class="comment">%   (at your option) any later version.</span>
0024 <span class="comment">%</span>
0025 <span class="comment">%   This program is distributed in the hope that it will be useful,</span>
0026 <span class="comment">%   but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
0027 <span class="comment">%   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
0028 <span class="comment">%   GNU General Public License for more details.</span>
0029 <span class="comment">%</span>
0030 <span class="comment">%   You can obtain a copy of the GNU General Public License from</span>
0031 <span class="comment">%   http://www.gnu.org/copyleft/gpl.html or by writing to</span>
0032 <span class="comment">%   Free Software Foundation, Inc.,675 Mass Ave, Cambridge, MA 02139, USA.</span>
0033 <span class="comment">%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</span>
0034 
0035 n=size(p,1);
0036 q=ones(n+1,3);
0037 q(1:n,1:2)=p;
0038 q(<span class="keyword">end</span>,:)=q(1,:);        <span class="comment">% duplicate the final point</span>
0039 cdist=q*l';
0040 cside=cdist&gt;0;           <span class="comment">% find which side each vertex lies</span>
0041 cside(end)=cside(1);        <span class="comment">% just in case</span>
0042 ec=find(cside(2:end)~=cside(1:n)); <span class="comment">% sides of boundary that are crossed</span>
0043 nc=numel(ec);
0044 <span class="keyword">if</span> ~nc
0045     xc=[];
0046     tc=[];
0047     ec=[];
0048     xy0=[];
0049 <span class="keyword">else</span>
0050     l2=l(1:2);
0051     l2m=l2*l2';
0052     l3=l(3);
0053     a=[-l(2) l(1)];
0054     xy0=-l3/(l2*l2')*l2;                <span class="comment">% point on line closest to origin</span>
0055     tn=(q(:,1:2)-repmat(xy0,n+1,1))*a'/l2m;
0056     tc=(cdist(ec+1).*tn(ec)-cdist(ec).*tn(ec+1))./(cdist(ec+1)-cdist(ec));
0057     [tc,ix]=sort(tc);
0058     ec=ec(ix);
0059     tm=tc(2:end)==tc(1:end-1); <span class="comment">% check for duplicated points</span>
0060     tm=[0;tm]|[tm;0];
0061     tc(tm)=[];                  <span class="comment">% remove duplicate pairs</span>
0062     ec(tm)=[];
0063     nc=numel(ec);
0064     xc=repmat(xy0,nc,1)+tc*a;
0065 <span class="keyword">end</span>
0066 <span class="keyword">if</span> ~nargout &amp;&amp; nc&gt;0
0067     plot(q(:,1),q(:,2),<span class="string">'-r'</span>,xc(:,1),xc(:,2),<span class="string">'-xb'</span>,xc(1,1),xc(1,2),<span class="string">'ob'</span>)
0068 <span class="keyword">end</span></pre></div>
<hr><address>Generated by <strong><a href="http://www.artefact.tk/software/matlab/m2html/">m2html</a></strong> &copy; 2003</address>
</body>
</html>